package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityUpgradePlan;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoUpgradePlan;
import com.ocom.payhandle.service.UpgradePlanService;
import com.ocom.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class UpgradePlanServiceImpl extends ServiceImpl<IDaoUpgradePlan, EntityUpgradePlan> implements UpgradePlanService {

    @Autowired
    private IDaoUpgradePlan upgradePlan;


    @Resource
    RedisUtil redisUtil;

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB, isMaster = MasterSlaveEnum.SLAVE)
    public String getScaleUpgradeStatus(Long companyId, String upgradeCode) {
        Object o = redisUtil.get("ScaleUpgradeStatus:" + upgradeCode+"#comId:"+companyId);
        if (null != o) {
            return (String) o;
        }

        List<EntityUpgradePlan> list = upgradePlan.getUpgradeVersion(upgradeCode);

        if (list == null || list.size() == 0) {
            return "";
        }
        for (EntityUpgradePlan entityUpgradePlan : list) {
            EntityUpgradePlan entityUpgradePlan1 = this.getById(entityUpgradePlan.getId());
            boolean fl = getScaleUpgradeStatus(companyId.intValue(),entityUpgradePlan1);
            if(fl){
                redisUtil.set("ScaleUpgradeStatus:" + upgradeCode+"#comId:"+companyId,entityUpgradePlan.getMsVer(),4 * 60 * 60l);
                return entityUpgradePlan.getMsVer();
            }
        }
        //取最小的版本号
        String version = null;
        for (EntityUpgradePlan entityUpgradePlan : list) {
            if(version==null){
                version = entityUpgradePlan.getMsVer();
                continue;
            }
            if(entityUpgradePlan.getMsVer().compareTo(version)<0){
                version = entityUpgradePlan.getMsVer();
            }
        }
        return version;
    }


    public boolean getScaleUpgradeStatus(Integer comId,EntityUpgradePlan entityUpgradePlan) {
        String[] upgradeComIds = entityUpgradePlan.getUpgradeComids().split(",");
        String[] backComIds = entityUpgradePlan.getBackComids().split(",");
        if (check(upgradeComIds, comId) && !check(backComIds, comId)) {
            return true;
        } else if (check(backComIds, comId)) {
            return false;
        } else {
            return false;
        }
    }

    private static boolean check(String[] targets, Integer comId) {
        boolean result = false;
        try {
            for (String target : targets) {
                if (target.startsWith(">=")) {
                    if (comId >= Integer.parseInt(target.substring(2))) {
                        result = true;
                    }
                } else if (target.startsWith("<=")) {
                    if (comId <= Integer.parseInt(target.substring(2))) {
                        result = true;
                    }
                } else if (target.startsWith(">")) {
                    if (comId > Integer.parseInt(target.substring(1))) {
                        result = true;
                    }
                } else if (target.startsWith("<")) {
                    if (comId < Integer.parseInt(target.substring(1))) {
                        result = true;
                    }
                } else if (target.contains("-")) {
                    String[] between = target.split("-");
                    if (2 == between.length) {
                        if (comId >= Integer.parseInt(between[0]) && comId <= Integer.parseInt(between[1])) {
                            result = true;
                        }
                    }
                } else if (comId == Integer.parseInt(target)) {
                    result = true;
                }
                if (result) {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    @Override
    @DataSourceAspect
    public EntityUpgradePlan findListByUpgradeCode(String upgradeCode) {
        QueryWrapper<EntityUpgradePlan> queryWrapper = Wrappers.query();
        queryWrapper.eq("upgrade_code", upgradeCode);
        queryWrapper.orderByDesc("plan_time");
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

}
